﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Prism.UnityExtensions;
using Microsoft.Practices.Prism.Modularity;
using System.Windows;
using Abreu.Base;
using CorpAbreu.Login;
using CorpAbreu.Login.ServiceReference1;
using CorpAbreu.Login.ViewModels;

namespace Abreu.Base
{
    public class LoginBootstrapper : UnityBootstrapper
    {
        #region Method Overrides

        /// <summary>
        /// Populates the Module Catalog.
        /// </summary>
        /// <returns>A new Module Catalog.</returns>
        /// <remarks>
        /// This method uses the Module Discovery method of populating the Module Catalog. It requires
        /// a post-build event in each module to place the module assembly in the module catalog
        /// directory.
        /// </remarks>
        protected override IModuleCatalog CreateModuleCatalog()
        {
            //var moduleCatalog = new DirectoryModuleCatalog();
            //moduleCatalog.ModulePath = @".\Modules";
            //return moduleCatalog;

            ModuleCatalog modcat = new ModuleCatalog();

            Type moduleCType = typeof(LoginModule);
            modcat.AddModule(
              new ModuleInfo()
              {
                  ModuleName = moduleCType.Name,
                  ModuleType = moduleCType.AssemblyQualifiedName,
              });

            return modcat;
        }

        /// <summary>
        /// Instantiates the Shell window.
        /// </summary>
        /// <returns>A new ShellWindow window.</returns>
        protected override DependencyObject CreateShell()
        {
            /* This method sets the UnityBootstrapper.Shell property to the ShellWindow
             * we declared elsewhere in this project. Note that the UnityBootstrapper base 
             * class will attach an instance of the RegionManager to the new Shell window. */


            return Container.TryResolve<Login>();
        }

        /// <summary>
        /// Displays the Shell window to the user.
        /// </summary>
        protected override void InitializeShell()
        {
            base.InitializeShell();

            App.Current.MainWindow = (Window)this.Shell;
            Application.Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;
            bool? dialogResult = App.Current.MainWindow.ShowDialog();

            if (dialogResult.HasValue && dialogResult.Value)
            {
                Application.Current.ShutdownMode = ShutdownMode.OnLastWindowClose;
                var bootstrapper = new Bootstrapper();
                bootstrapper.Run();
            }
            else
            {
                Application.Current.Shutdown(0);
            }
        }

        #endregion
    }
}
